import 'package:city_corner/commons/global-var.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DragBtn extends StatefulWidget {
  final double width;
  final double height;
  final double startX;
  final double endX;
  final bool reverse;
  final double minX;
  final double maxX;
  final dynamic setRange;
  DragBtn(
      {this.width = rentBtnSize,
      this.height = rentBtnSize,
      this.startX = -1,
      this.endX = -1,
      this.minX,
      this.maxX,
      this.setRange,
      this.reverse = false});

  @override
  _DragBtnState createState() => _DragBtnState();
}

class _DragBtnState extends State<DragBtn> {
  // 静止状态下的offset
  Offset idleOffset = Offset(0, 0);
  // 本次移动的offset
  Offset moveOffset = Offset(0, 0);
  // 最后一次down事件的offset
  Offset lastOffset = Offset(0, 0);

  @override
  void initState() {
    super.initState();
    setState(() {
      moveOffset = Offset(widget.startX + widget.minX, 0);
    });
  }

  @override
  void didUpdateWidget(DragBtn oldWidget) {
    super.didUpdateWidget(oldWidget);
    setState(() {
      moveOffset = Offset(widget.reverse ? widget.endX : widget.startX, 0);
    });
    if (widget.reverse) {
      print(widget.endX.toString() + ' ' + widget.startX.toString());
    }
  }

  double leftToRight(double moveDx) {
    if (widget.endX > 0 && moveDx > widget.endX) {
      moveDx = widget.endX;
    }
    // if (moveDx < widget.startX) {
    //   widget.setRange('startX', moveDx);
    // }
    return moveDx;
  }

  double rightToLeft(double moveDx) {
    if (widget.startX > 0 && moveDx < widget.startX) {
      moveDx = widget.startX;
    }
    // if (moveDx > widget.endX) {
    //   widget.setRange('endX', moveDx);
    // }
    return moveDx;
  }

  double range(double moveDx) {
    if (moveDx > widget.maxX) {
      moveDx = widget.maxX;
    }
    if (moveDx < widget.minX) {
      moveDx = widget.minX;
    }
    return moveDx;
  }

  @override
  Widget build(BuildContext context) {
    return Transform.translate(
      offset: moveOffset,
      child: Container(
        margin: EdgeInsets.only(bottom: 20),
        child: GestureDetector(
          onPanStart: (details) {
            setState(() {
              lastOffset = Offset(details.globalPosition.dx, 0);
            });
          },
          onPanUpdate: (details) {
            double moveDx = details.globalPosition.dx;
            // 终点在起始点的右边
            if (!widget.reverse) {
              moveDx = leftToRight(moveDx);
            } else {
              moveDx = rightToLeft(moveDx);
            }
            setState(() {
              moveOffset = Offset(moveDx, 0);
            });
            moveDx = range(moveDx);
          },
          onPanEnd: (details) {
            double x = moveOffset.dx;
            setState(() {
              idleOffset = Offset(x, 0);
            });
          },
        ),
        width: widget.width,
        height: widget.height,
        decoration: BoxDecoration(
            color: Colors.red,
            borderRadius: BorderRadius.all(Radius.circular(25))),
      ),
    );
  }
}
